/*
 * Copyright 2012 Sergey Netesanyi.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.versatile.gwt.serialization.client.stream;

import com.google.gwt.user.client.rpc.SerializationException;
import com.google.gwt.user.client.rpc.SerializationStreamWriter;
import com.google.gwt.user.client.rpc.impl.Serializer;
import com.versatile.gwt.serialization.client.Constants;

/**
 * {@link SerializationStreamWriter} implementation to deserialize objects from string on the client side.
 *
 * @author Sergey Netesanyi
 * @version 1.0
 */
public class StringSerializationStreamWriter implements SerializationStreamWriter {
    /** Stream object. */
    private StringBuffer stream =  new StringBuffer();
    /** Serializer object. */
    private Serializer serializer;

    /** {@inheritDoc} */
    @Override
    public void writeBoolean(boolean value) throws SerializationException {
        append(value);
    }

    private void append(Object value) {
        stream.append(value).append(Constants.SEPARATOR);
    }

    /** {@inheritDoc} */
    @Override
    public void writeByte(byte value) throws SerializationException {
        append(value);
    }

    /** {@inheritDoc} */
    @Override
    public void writeChar(char value) throws SerializationException {
        append(value);
    }

    /** {@inheritDoc} */
    @Override
    public void writeDouble(double value) throws SerializationException {
        append(value);
    }

    /** {@inheritDoc} */
    @Override
    public void writeFloat(float value) throws SerializationException {
        append(value);
    }

    /** {@inheritDoc} */
    @Override
    public void writeInt(int value) throws SerializationException {
        append(value);
    }

    /** {@inheritDoc} */
    @Override
    public void writeLong(long value) throws SerializationException {
        append(value);
    }

    /** {@inheritDoc} */
    @Override
    public void writeObject(Object value) throws SerializationException {
        if (value == null) {
            writeString("");
            return;
        }

        Class<?> clazz = value.getClass();

        if (value instanceof Enum<?>) {
            Enum<?> e = (Enum<?>) value;
            clazz = e.getDeclaringClass();
        }

        String signature = serializer.getSerializationSignature(clazz);
        if (signature == null) {
            throw new SerializationException("Type " + clazz.getName()
                    + " is not included in 'serialization.allowedList'");
        }
        writeString(signature);
        serializer.serialize(this, value, signature);
    }

    /** {@inheritDoc} */
    @Override
    public void writeShort(short value) throws SerializationException {
        append(value);
    }

    public StringSerializationStreamWriter(Serializer serializer) {
        this.serializer = serializer;
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        return stream.toString();

    }

    /** {@inheritDoc} */
    @Override
    public void writeString(String value) throws SerializationException {
        append(value);
    }
}
